package mscontrol;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.dao.MajorDao;
import model.entity.Major;

/**
 * Servlet implementation class GetMSMajor
 */
@WebServlet("/GetMSMajor")
public class GetMSMajor extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetMSMajor() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Enumeration<String> majorPara=request.getParameterNames();
		String condit=(String)request.getParameter("condit");

		 Major major=new Major();
		 
		 for(int i=0;i<2;i++){
			 String paraName=(String)majorPara.nextElement();
			 String paraVal=request.getParameter(paraName);
			 System.out.println(paraName + " " + paraVal);
			 if(paraVal!=""){
			     switch (i) {
				case 0:
					major.setId(Integer.valueOf(paraVal));
					break;
				case 1:
					major.setName(paraVal);
					break;
				default:
					break;
				}
			 }
		 }
		 
		 MajorDao td=new MajorDao();
		 List<Major> list=td.getRanSelect(major, condit);
        
		 String str="[";
		 for (Major ma : list) {
			str+="{\"ID\":" + ma.getId() + ",\"Name\":\"" + ma.getName() + "\"},";
		 }
		 
		 str=str.substring(0, str.length()-1);
		 str+="]";
		 response.setCharacterEncoding("utf-8");
		 PrintWriter out=response.getWriter();
		 out.print(str);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
